//
//  XHBBaseTabBarController.m
//  iOS Object
//
//  Created by 保密 on 2019/6/26.
//  Copyright © 2019年 XiaoHaiBao. All rights reserved.
//

#import "XHBBaseTabBarController.h"

@interface XHBBaseTabBarController ()<UITabBarControllerDelegate>

@end

@implementation XHBBaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tabbar = [[XHBTabBar alloc] init];
    
    [_tabbar.centerBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //利用KVC 将自己的tabbar赋给系统tabBar
    [self setValue:_tabbar forKeyPath:@"tabBar"];
    
    self.delegate = self;
    
}

// 重写选中事件， 处理中间按钮选中问题
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
    _tabbar.centerBtn.selected = (tabBarController.selectedIndex == self.viewControllers.count/2);
    
    if (self.xhb_delegate){
        [self.xhb_delegate tabBarController:tabBarController didSelectViewController:viewController];
    }
}

- (void)buttonAction:(UIButton *)button{
    
    NSInteger count = self.viewControllers.count;
    
    self.selectedIndex = count/2;//关联中间按钮
    
    [self tabBarController:self didSelectViewController:self.viewControllers[self.selectedIndex]];
}

@end
